//JavaScript Framework 2.0 Code
try {
	Type.registerNamespace('com.yonyou.ncmm_pac_pda.MaterialOutScanListController');
	com.yonyou.ncmm_pac_pda.MaterialOutScanListController = function() {
		com.yonyou.ncmm_pac_pda.MaterialOutScanListController.initializeBase(this);
		this.initialize();
	}

	function com$yonyou$ncmm_pac_pda$MaterialOutScanListController$initialize() {
	}

	function com$yonyou$ncmm_pac_pda$MaterialOutScanListController$evaljs(js) {
		eval(js)
	}

	//返回按钮
	function com$yonyou$ncmm_pac_pda$MaterialOutScanListController$back(sender, args) {
		$view.close({
			"resultCode": "15"
		});
	}

	//右上角公共菜单
	function loadmenu(sender, args) {
		NCMMPubMenu.openDropDownList("imagebutton0");
	}

	// 列表行单击事件 -- 弹出浮动按钮组
	function onitemclick(sender, args) {
		//如果点击的是组列表，则不弹出浮动按钮组
		var row = {};
		row = $id("listviewdefine0").get("row");
		row = $stringToJSON(row);
		if(row && row["row"])
			return;

		$view.openPop(NCMMMOUtil.popDialogInfo("panel35"));
	}

	//浮动按钮组 -- 详情
	function onclick_Detail() {
		//先关闭按钮组
		onclick_Cancel();

		// 获取当前行内容
		var row = {};
		row = $id("listviewdefine0").get("row");

		// 单击行跳转到详情页面
		row = $stringToJSON(row);
		$view.open({
			"viewid": "com.yonyou.ncmm_pac_pda.MaterialOutScanDetail",
			"isKeep": "true",
			"row": row
		});

	}

	//浮动按钮组 -- 修改
	function onclick_Modify() {
		//先关闭按钮组
		onclick_Cancel();

		// 获取当前行内容
		var rowData = $id("listviewdefine0").get("row");
		rowData = $stringToJSON(rowData);

		var bodydata = {};
		bodydata[MaterialOutScansVO.VBILLCODE.name] = rowData[MaterialOutScansVO.VBILLCODE.name];
		bodydata[MaterialOutScansVO.CROWNO.name] = rowData[MaterialOutScansVO.CROWNO.name];
		bodydata[MaterialOutScansVO.TS.name] = rowData[MaterialOutScansVO.TS.name];
		
		var row = {
			"data": bodydata,
			"type": "4" //图4 --> 图3 （修改）
		};
		$view.open({
			viewid: "com.yonyou.ncmm_pac_pda.MaterialOutScan",
			isKeep: "false",
			row: row
		});

	}

	//浮动按钮组 -- 删除
	function onclick_Delete() {
		//先关闭按钮组
		onclick_Cancel();

		//询问是否删除
		var params = {
			"title": "扫描记录",
			"message": "确定删除当前扫描记录吗？",
			"okbuttontitle": "确定",
			"cancelbuttontitle": "取消",
			"style": "ok-cancel",
			"okaction": "doDelete()",
			"cancelaction": "doCancel()"
		};
		$view.openDialog(params);
	}

	//浮动按钮组 -- 取消
	function onclick_Cancel() {
		// 取消
		$view.closePop(NCMMMOUtil.popDialogInfo("panel35"));
	}

	//删除行 -- 确定
	function doDelete() {
		// 获取当前行内容
		var scanvo = {};
		scanvo = $id("listviewdefine0").get("row");
		scanvo = $stringToJSON(scanvo);

		//删除扫描记录表当前行
		var condition = MaterialOutScansVO.VBILLCODE.name + "='" + scanvo[MaterialOutVO.VBILLCODE.name] +
			"' and " + MaterialOutScansVO.CROWNO.name + "= '" + scanvo[MaterialOutVO.CROWNO.name] +
			"' and " + MaterialOutScansVO.TS.name + "= '" + scanvo[MaterialOutScansVO.TS.name] + "'";

		NCMMPubUtil.batchDeleteByCondition(MaterialOutScansVO, condition);

		//更新上游单据表
		NCMMMOUtil.updateMaterialOutRecord(scanvo[MaterialOutVO.VBILLCODE.name], scanvo[MaterialOutVO.CROWNO.name]);

		//更新界面显示
		loadAllScanRecords();

	}

	//删除行 -- 取消
	function doCancel() {
		$toast("删除操作已取消。");
	}

	//组checkbox 单击事件
	function onClickGroup(sender, args) {
		$ctx.dataCollect();
		var isCheckedAll = true;

		var sourceData = $ctx.getJSONArray("source");
		for(var i = 0; i < sourceData.length; i++) {
			if(sourceData[i]["isgroupchecked"] == null || sourceData[i]["isgroupchecked"] == false || sourceData[i]["isgroupchecked"] == "false") {
				isCheckedAll = false;
				for(var j = 0; j < (sourceData[i]["row"]).length; j++) {
					((sourceData[i]["row"])[j])["ischecked"] = false;
				}
			} else {
				for(var j = 0; j < (sourceData[i]["row"]).length; j++) {
					((sourceData[i]["row"])[j])["ischecked"] = true;
				}
			}
		}
		$ctx.put("isAllChecked", isCheckedAll); //更新全选复选框的界面显示
		$ctx.put("source", sourceData);
		$ctx.dataBind();
	}

	//列表行checkbox 单击事件（暂时不支持）
	function onClickItem(sender, args) {
		//组内条目复选框只能看不能操作

	}

	//全选checkbox 单击事件
	function onAllClick(sender, args) {
		$ctx.dataCollect();
		var isChecked = $id(sender).get("checked");
		var sourceData = $ctx.getJSONArray("source");
		for(var i = 0; i < sourceData.length; i++) {
			sourceData[i]["isgroupchecked"] = isChecked;

			for(var j = 0; j < (sourceData[i]["row"]).length; j++) {
				((sourceData[i]["row"])[j])["ischecked"] = isChecked;
			}
		}
		$ctx.put("source", sourceData);
		$ctx.dataBind();

	}

	//页面onrestart事件
	function pageRestart(sender, args) {
		loadAllScanRecords();
	}

	//列表加载事件
	function com$yonyou$ncmm_pac_pda$MaterialOutScanListController$listviewdefine0_onload(sender, args) {
		loadAllScanRecords();
	}

	//从本地表获取所有材料出库扫描记录并显示
	function loadAllScanRecords() {
		var sql = getAllTableQuerySql();
		var scangroup = getVOsBySql(sql);
		//补齐小数位
		scangroup = NCMMMOUtil.changeDecimalFiguresByMeascdoc(MaterialOutVO, scangroup);

		//显示
		$ctx.push({
			"source": scangroup
		});
	}

	//返回全表查询sql，用单据号+行号 排序
	function getAllTableQuerySql() {
		var sql = "SELECT " +
			MaterialOutScansVO.VBILLCODE.name + ", " + MaterialOutScansVO.CROWNO.name + ", " +
			MaterialOutScansVO.CINVCODE.name + ", " + MaterialOutScansVO.CINVNAME.name + ", " +
			MaterialOutScansVO.NSHOULDINNUM.name + ", " + MaterialOutScansVO.TNAME.name + ", " +
			MaterialOutScansVO.NSHOULDASSISTNUM.name + ", " + MaterialOutScansVO.FNAME.name + ", " +
			MaterialOutScansVO.PKMEASDOC.name + ", " + MaterialOutScansVO.CASTUNITID.name +	  
			", sum("+ MaterialOutScansVO.SCANNUM.name + ") as groupsum" 	+
			" FROM " + MaterialOutScansVO.prototype.getTableName() +
			" group by " + MaterialOutScansVO.VBILLCODE.name + ", " + MaterialOutScansVO.CROWNO.name;

		return sql;
	}

	//根据sql获取本地表vos
	function getVOsBySql(sql) {
		NCMMPubUtil.createTableWithAutoPK(MaterialOutScansVO);

		var data = NCMMPubUtil.queryBySQL(sql);
		data = NCMMPubUtil.htmlspecialjsonarray2UI(data);

		for(var i = 0; i < data.length; i++) {
			var vbillcode = data[i][MaterialOutScansVO.VBILLCODE.name];
			var crowno = data[i][MaterialOutScansVO.CROWNO.name];
			var wheresql = "where " + MaterialOutScansVO.VBILLCODE.name + "= '" + vbillcode + "' and " + MaterialOutScansVO.CROWNO.name + "= '" + crowno + "'";
			NCMMPubUtil.queryVO(MaterialOutScansVO, wheresql, function(subdata) {
				subdata = eval(subdata);
				subdata = NCMMPubUtil.htmlspecialjsonarray2UI(subdata);
				//$alert("subdata.length=" + subdata.length);
				data[i]["row"] = subdata;
			});

		}

		return data;
	}
	
	
	//删除事件
	//按单据行逐个发送NC请求
	function onSubmit(sender, args) {
		$ctx.dataCollect();
		var isCheckedAll = true;

		var sourceData = $ctx.getJSONArray("source");
		var noRecordCheck = true;
		for(var i = 0; i < sourceData.length; i++) {
			//			$alert($jsonToString(sourceData[i]));
			if(sourceData[i]["isgroupchecked"] == null || sourceData[i]["isgroupchecked"] == false || sourceData[i]["isgroupchecked"] == "false") {
				continue;
			} else {
				var scanvo = (sourceData[i]["row"])[0];
				var totalnum = NCMMMOUtil.getSumScannum(scanvo[MaterialOutScansVO.VBILLCODE.name], scanvo[MaterialOutScansVO.CROWNO.name]);
				if(totalnum == null || totalnum["scannum"] == null)
					scanvo[MaterialOutScansVO.SCANNUM.name] = "";
				else
					scanvo[MaterialOutScansVO.SCANNUM.name] = totalnum["scannum"];

				//获取当前扫描记录对应的上游单据vo
				var outvo = NCMMMOUtil.getOriginalOutvo(scanvo[MaterialOutScansVO.VBILLCODE.name], scanvo[MaterialOutVO.CROWNO.name]);
				if(outvo == null) {
					continue;
				}

				//组装提交数据
				var billinfos = NCMMMOUtil.getSubmitData(outvo, scanvo);
				if(billinfos == null) {
					continue;
				}
				noRecordCheck = false;
				//提交数据到NC
				NCMMMOUtil.submitInfo2NC(billinfos, outvo, loadAllScanRecords);
			}
		}

		if(noRecordCheck == true) {
			$toast("没有选中扫描记录！");
		}
	}


	com.yonyou.ncmm_pac_pda.MaterialOutScanListController.prototype = {
		listviewdefine0_onload: com$yonyou$ncmm_pac_pda$MaterialOutScanListController$listviewdefine0_onload,
		back: com$yonyou$ncmm_pac_pda$MaterialOutScanListController$back,
		initialize: com$yonyou$ncmm_pac_pda$MaterialOutScanListController$initialize,
		evaljs: com$yonyou$ncmm_pac_pda$MaterialOutScanListController$evaljs
	};
	com.yonyou.ncmm_pac_pda.MaterialOutScanListController.registerClass('com.yonyou.ncmm_pac_pda.MaterialOutScanListController', UMP.UI.Mvc.Controller);
} catch(e) {
	$e(e);
}